﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using CellphoneRepair.DAL;
using CellphoneRepair.Entities;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Base;

namespace CellphoneRepair.UserControls
{
    public partial class uc_Customers : Uc_list_base
    {
        public uc_Customers()
        {
            InitializeComponent();
        }

        private void uc_Customers_Load(object sender, EventArgs e)
        {
            bindingSource1.DataSource = typeof (Kehu);

            using (KehuBll bll = new KehuBll())
            {
                bindingSource1.DataSource = bll.GetAll();
            }

            bindingSource1.AddingNew += bindingSource1_AddingNew;
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void bindingSource1_AddingNew(object sender, AddingNewEventArgs e)
        {
            Kehu kehu = new Kehu();

            kehu.Idkehu = Commons.GenerateIdByForeTag("KH");

            e.NewObject = kehu;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        internal override void simpleButton3_Click(object sender, EventArgs e)
        {
            base.simpleButton3_Click(sender, e);

            if (XtraMessageBox.Show("是否确定要删除这条记录？", "提示", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                using (KehuBll bll = new KehuBll())
                {
                    bll.Delete(bindingSource1.Current as Kehu);
                    bindingSource1.RemoveCurrent();
                }
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        internal override void gridView1_RowUpdated(object sender, RowObjectEventArgs e)
        {
            base.gridView1_RowUpdated(sender, e);
            using (KehuBll bll = new KehuBll())
            {
                switch (editType)
                {
                    case EnumEditType.Create:
                        bll.Insert(bindingSource1.Current as Kehu);
                        break;
                    case EnumEditType.Edit:
                        bll.Update(bindingSource1.Current as Kehu);
                        break;
                }
            }
        }
    }
}